package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.UserDao;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.PermissionService;
import com.itheima.service.RoleService;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

/**
 * @author hql
 * @date 2019-12-11 18:42
 */
@Service(interfaceClass = UserService.class)
@Transactional
public class UserserviceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleService roleService;
    @Autowired
    private PermissionService permissionService;
    /**
     * 根据用户名查询是否存在该用户
     * @param username
     * @return
     */
    @Override
    public User findByUsername(String username) {
        //再java代码中实现分布查询
        //根据用户名查询User表
        User user = userDao.findByUsername(username);
        if (user == null){
            return null;
        }
        //根据User的id查询出所有的Role角色
        Set<Role> roleList= roleService.findByUserId(user.getId());
        //根据Role的id查询出所有的权限
        if (roleList!=null&&roleList.size()>0) {
            for (Role role : roleList) {
               Set<Permission> permissionList = permissionService.findByRoleId(role.getId());
                if (permissionList!=null && permissionList.size()>0){
                    role.setPermissions(permissionList);
                }
            }
            user.setRoles(roleList);
        }



        return user;
    }
}
